/**
 * 流端口测试示例
 * 验证流地址生成是否正确使用了 streamPlaybackPort
 */

import { usePlaybackStore } from '@/stores/playback'

export async function testStreamPortUsage() {
  const playbackStore = usePlaybackStore()
  
  // 模拟设备数据
  const mockCamera = {
    id: 1,
    name: '测试摄像头',
    nvrId: 1,
    subEquId: 1,
    equId: 0,
    ip: '192.168.1.100',
    port: 80,
    protocol: 'RTSP',
    status: 1,
    ptzEnable: false
  }
  
  // 模拟流媒体服务器数据
  playbackStore.streamServer = [
    {
      id: 1,
      ip: '192.168.3.231',
      port: 25080,
      name: '录像服务器1'
    }
  ]
  
  // 设置端口
  playbackStore.streamPort = '25080'        // 录像查询端口
  playbackStore.streamPlaybackPort = '25081' // 回放流端口
  
  console.log('测试配置:')
  console.log('录像查询端口 (streamPort):', playbackStore.streamPort)
  console.log('回放流端口 (streamPlaybackPort):', playbackStore.streamPlaybackPort)
  
  try {
    // 测试生成流地址
    const streamUrl = await playbackStore.generatePlaybackStreamUrl(
      mockCamera,
      '2025-10-14',
      '10:30:00'
    )
    
    console.log('生成的流地址:', streamUrl)
    
    // 验证端口是否正确
    const expectedPort = playbackStore.streamPlaybackPort
    if (streamUrl.includes(`:${expectedPort}/`)) {
      console.log('✅ 流地址使用了正确的回放端口:', expectedPort)
    } else {
      console.error('❌ 流地址使用了错误的端口')
    }
    
    // 验证流地址格式
    const expectedFormat = `http://192.168.3.231:${expectedPort}/xylive/pb_d0_c1_s20251014T103000Z_e20251014T235959Z/`
    if (streamUrl.startsWith(expectedFormat)) {
      console.log('✅ 流地址格式正确')
    } else {
      console.error('❌ 流地址格式不正确')
      console.log('期望格式:', expectedFormat)
      console.log('实际格式:', streamUrl)
    }
    
  } catch (error) {
    console.error('测试失败:', error)
  }
}

/**
 * 批量测试多个摄像头的流地址生成
 */
export async function testBatchStreamGeneration() {
  const playbackStore = usePlaybackStore()
  
  // 模拟多个设备
  const mockCameras = [
    {
      id: 1,
      name: '摄像头1',
      nvrId: 1,
      subEquId: 1,
      equId: 0,
      ip: '192.168.1.100',
      port: 80,
      protocol: 'RTSP',
      status: 1,
      ptzEnable: false
    },
    {
      id: 2,
      name: '摄像头2',
      nvrId: 1,
      subEquId: 2,
      equId: 0,
      ip: '192.168.1.101',
      port: 80,
      protocol: 'RTSP',
      status: 1,
      ptzEnable: false
    }
  ]
  
  // 设置服务器和端口
  playbackStore.streamServer = [
    {
      id: 1,
      ip: '192.168.3.231',
      port: 25080,
      name: '录像服务器1'
    }
  ]
  playbackStore.streamPort = '25080'
  playbackStore.streamPlaybackPort = '25081'
  
  try {
    console.log('开始批量生成流地址...')
    const streamUrls = await playbackStore.generateBatchPlaybackStreamUrls(
      mockCameras,
      '2025-10-14',
      '10:30:00'
    )
    
    console.log('批量生成的流地址:')
    streamUrls.forEach((url, key) => {
      console.log(`${key}: ${url}`)
      
      // 验证端口
      if (url.includes(':25081/')) {
        console.log('  ✅ 使用了正确的回放端口')
      } else {
        console.log('  ❌ 端口不正确')
      }
    })
    
  } catch (error) {
    console.error('批量测试失败:', error)
  }
}

/**
 * 在Vue组件中使用示例
 */
export function useStreamPortInComponent() {
  const playbackStore = usePlaybackStore()
  
  const startPlayback = async (cameras: any[], date: string, time: string) => {
    try {
      // 确保端口已设置
      if (!playbackStore.streamPlaybackPort) {
        console.warn('回放端口未设置，请先初始化数据')
        return
      }
      
      console.log('使用回放端口:', playbackStore.streamPlaybackPort)
      
      // 生成流地址
      const streamUrls = await playbackStore.generateBatchPlaybackStreamUrls(
        cameras,
        date,
        time
      )
      
      // 处理流地址
      streamUrls.forEach((url, key) => {
        console.log(`摄像头 ${key} 的流地址: ${url}`)
        // 这里可以将流地址传递给视频播放器
      })
      
    } catch (error) {
      console.error('播放失败:', error)
    }
  }
  
  return {
    startPlayback,
    streamPort: playbackStore.streamPort,
    streamPlaybackPort: playbackStore.streamPlaybackPort
  }
}
